<?php
	require_once('server.cfg.php');
	define(STIMPy_INTERNAL,true);
	session_start();
	if(strcmp($this_page,'login'))
		$_SESSION['last_page']=$this_page;
	
	/*--------------------------------------\
	| These are all the required files for  |
	| each page. They should all be located |
	| in the 'includes' directory           |
	\--------------------------------------*/
	if(page_location=='local')
	{
		define('ROOT_DIR', 'h:/new web/');
	} elseif(page_location=='web') {
		define('ROOT_DIR', '/home/stimpy/public_html/');
	} elseif(page_location=='test') {
		//enter code here
	}
	require_once(ROOT_DIR. 'includes/smarty/Smarty.class.php');
	require_once('SQL.inc.php');
	//require_once('lang.inc.php');
	require_once('config.inc.php');
	require_once('classes.inc.php');

	// End Requires
	/* Change this to reflect your database setup */
	$dbcon = new database(db_server,db_user,db_password);
	if (!$dbcon) {
		exit('Could not connect to the database!');
	}

	// Check to see if user is logged in

	if(isset($_SESSION['logged_as_user']))
	{
		$logged=$_SESSION['logged_as_user'];
		$num_offlines=get_num_offlines(@$dbcon,$logged);
	}

	// End login check

	/*-----------------------------------------------\
	| This is where Smarty is setup and instantiated |
	\-----------------------------------------------*/

	$smarty = new Smarty();

	//$smarty->compile_check = true;
	//$smarty->debugging = true;

	$smarty->template_dir = ROOT_DIR.'templates'.(isset($_GET['new'])?'/new':'');
	$smarty->compile_dir = ROOT_DIR.'templates_c'.(isset($_GET['new'])?'/new':'');
	$smarty->force_compile=true;

	// End Smarty setup

	/*---------------------------------------\
	| Now that Smarty is setup, we can start |
	| assigning variables into the templates |
	\---------------------------------------*/
	!isset($_SESSION['browser_info'])?getUserBrowser():null;
	//print_r($_SESSION);
	//print_r($_SESSION['browser_info']['browser']);
	$smarty->assign('browser',$_SESSION['browser_info']['browser']);
	$smarty->assign('browser_ver',$_SESSION['browser_info']['ver']);
	$smarty->assign('browser_render',$_SESSION['browser_info']['render']);
	$smarty->assign('center',isset($center_main)?$center_main:false);
	$smarty->assign('cur_page_ver',$cur_page_ver);
	$smarty->assign('last_update',"8-1-2006 @ 12:00 AM Central Time");
	$smarty->assign('cur_web_ver',"0.7d");
	$smarty->assign('this_page',$this_page);
	$smarty->assign('title',$this_title);
	$smarty->assign('logged',$logged);
	$smarty->assign('num_offlines',$num_offlines);
	
	$result=$dbcon->query(SQL_get_OL_users);
	$smarty->assign('users_ol',(!$result)?0:count($result));
	//$result?mysql_free_result($result):null;
	
	$result=$dbcon->query(SQL_get_regd_users);
	$smarty->assign('users_regd',count($result));
	// Misc Functions

	function get_num_offlines($dbcon,$user)
	{
		return count($dbcon->query("SELECT offline_id from offlines left join users on offlines.to=users.UID where users.UN='".$user."'"));
	}
	function getUserBrowser()
	{
		//echo $_SERVER['HTTP_USER_AGENT']."<br>";
		$info=$_SERVER['HTTP_USER_AGENT'];
		$pattern="(MSIE [0-9].[0-9])";
		preg_match($pattern,$info, $render, PREG_OFFSET_CAPTURE);
		$render=count($render)==0?"Other":"IE";
		$pattern="([a-zA-Z]*[ /][0-9\.]*)";
		preg_match_all($pattern, $info, $browser, PREG_OFFSET_CAPTURE);
		//print_r($browser);
		$browser=$browser[0][count($browser[0])-1][0];
		$temp=explode(" ",$browser);
		if(count($temp)==1)
		{
			$browser=explode("/",$browser);
		} else {
			$browser=$temp;
		}
		$ver=$browser[1];
		$browser=$browser[0];
		if($browser=="CLR")
		{
			$pattern="(MSIE [0-9].[0-9])";
			preg_match($pattern,$info, $temp, PREG_OFFSET_CAPTURE);
			$temp=explode(" ",$temp[0][0]);
			$browser=$temp[0];
			$ver=$temp[1];
		}	
		$return=  array('browser' => $browser,
						'render' => $render,
						'ver' => $ver,
						'header' => $info);
		$_SESSION['browser_info']=$return;
	}
	/*----------------------------------------------\
	| Here we start the code for the download panel |
	\----------------------------------------------*/
	$memberofdev=isset($_SESSION['uid'])?$dbcon->getMemberOfGroup($_SESSION['uid'],'Developers'):false;
	//echo isset($_SESSION['uid']);
	//echo $_SESSION['uid'];
	//echo $_SESSION['uid'][0];
	//echo $memberofdev;
	$smarty->assign('member_of_dev',$memberofdev);
	
?>